import router, { asyncRoutes } from '@/router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
const whiteList = ['/login', '404']
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  document.title = getPageTitle(to.meta.title)
  console.log(from.path, '到', to.path)
  const token = store.state.user.token
  if (token) {
    // 已经登录
    if (to.path === '/login') {
      console.log('已经登录了，跳转到首页')
      next('/')
      NProgress.done()
    } else {
      console.log('已经登录，直接放行')
      // 判断有没有userId 没有就调用
      if (!store.getters.userId) {
        // await store.dispatch('user/initUserInfo')
        // 获取所有展示路由权限名称
        const menus = await store.dispatch('user/initUserInfo')
        // 对全部路由列表进行筛选 筛选出在menu中的路由
        const filterRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
        // 将404追加到路由的最后面
        filterRoutes.push({ path: '*', redirect: '/404', hidden: true })
        // 动态添加路由
        router.addRoutes(filterRoutes)
        // 添加动态路由到vuex-menu中
        store.commit('menu/setMenuList', filterRoutes)
        // 路由守卫是异步的,我们在动态添加路由,会产生一个bug.陆游的添加,包括vuex中的数据渲染,没有完成就直接跳转页面了
        next({
          ...to, // 路由添加完了在进入页面
          repalce: true // 重新进一次, 不保留重复历史
        })
      }
      // 有userId就放行
      next()
    }
  } else {
    // 没有登录
    // 判断是否有白名单
    if (whiteList.includes(to.path)) {
      console.log('拥有白名单')
      next()
    } else {
      console.log('没有白名单')
      next('/login')
      NProgress.done()
    }
  }
})

// 跳转完成的钩子函数afterEach()
router.afterEach((to, from) => {
  NProgress.done()
})

// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
